package com.example.gc.myapplication;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;

import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;



public class MainActivity extends Activity implements ServiceConnection {
    Intent mIntent1 ;
    Intent mIntent2;
    MyBindService service;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    public void doClick(View v){
        switch (v.getId()){
            case R.id.startservice:
                 mIntent1 = new Intent(MainActivity.this,MyService.class);
                startService(mIntent1);
                break;
            case R.id.stopservice:
                mIntent1 = new Intent(MainActivity.this,MyService.class);
                stopService(mIntent1);
                break;
            case R.id.bind:
                mIntent2 = new Intent(this,MyBindService.class);
                bindService(mIntent2,this, Service.BIND_AUTO_CREATE);
                break;
            case R.id.play:
                service.play();
                break;
            case R.id.stop:
                service.stop();
                break;
            case R.id.next:
                service.next();
                break;
            case R.id.last:
                service.last();
                break;
            case R.id.unbind:
                unbindService(this);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        stopService(mIntent2);
        unbindService(this);
        super.onDestroy();
    }

    //当启动源跟Service成功连接之后会自动调用这个方法
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        //通过MyBindService的内部类得到整个Service实例,然后就可以在Activity里面调用service中的方法
        service = ((MyBindService.MyBinder)binder).getService();
    }
    //当启动源跟Service的连接意外丢失的时候回调用这个方法
    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
}
